/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.BUS;

import Server.DTO.DeleteHaveWhereQuery;
import Server.DTO.DeleteNoWhereQuery;
import Server.DTO.UpdateHaveWhereQuery;
import Server.DTO.UpdateQuery;
import Server.DTO.UpdateNoWhereQuery;
import Server.DTO.Query;
import java.io.File;
import java.io.FileInputStream;
import javax.xml.*;
import java.util.ArrayList;
import java.util.List;
import Server.DAO.DocumentUlti;
import Server.DAO.DatabaseAccess;
import Server.DTO.*;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.NodeList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 *
 * @author TRANDINHDUY
 */
public class QueryBUS_Update {
      ///cac cong viec con lai:
      ///Delete:
        //get FromString: lay string cac table
        ///Xu ly menh de where: giong nhu Update
        //cui cung la truy xuat xml!
    
    ///Update: chinh sua get
    //Test ham
    //viet ham lay list cac menh de set!
      private Query query;
      private UpdateHaveWhereQuery updateHave=null;
      private UpdateNoWhereQuery updateNo=null;
      private DeleteHaveWhereQuery deleteHave=null;
      private DeleteNoWhereQuery deleteNo=null;
      private List<String> UpdateOperatorList;//list chi and hay or
      private List<String> DeleteOperatorList;
      
      public QueryBUS_Update(String stringInput){
          UpdateOperatorList=new ArrayList<String>();
          DeleteOperatorList=new ArrayList<String>();
          StringUlti stringulti=new StringUlti();
          stringInput=stringulti.formatString(stringInput);
          QueryUlti ulti=new QueryUlti();
          query=new Query(stringInput, ulti.checkQueryType(stringInput)) {};
          if(query.getQueryString().indexOf("UPDATE")>=0){
            if(setStyleUpdate()){
              updateHave=new UpdateHaveWhereQuery(stringInput); 
            }
            else{
              updateNo=new UpdateNoWhereQuery(stringInput);
            }
          }   
          else{
              if(setStyleDelete()){
              deleteHave=new DeleteHaveWhereQuery(stringInput); 
            }
            else{
              deleteNo=new DeleteNoWhereQuery(stringInput);
            }
          }
      }
      
      public boolean setStyleUpdate(){
          if(query.getQueryString().indexOf("WHERE")>0)
              {
              
              return true; 
          }
          
          else
          {
              
              return false;
          }
          
      }
      public boolean setStyleDelete(){
          if(query.getQueryString().indexOf("WHERE")>0){
              
              return true; 
          }
          
          else{
              
              return false;
          }      
      }
      public String tableNameUpdate()
      {
          
          String temp="";
          int begin=0;
          int end=0;
          String tableStr="";
          if(setStyleUpdate()){
             temp=updateHave.getQueryString();
             begin=temp.indexOf(" ");
             end=temp.indexOf("SET");
             tableStr=temp.substring(begin, end).replaceAll(" ","");
          }
          else{
              temp=updateNo.getQueryString();
              begin=temp.indexOf(" ");
              end=temp.indexOf("SET");
              tableStr=temp.substring(begin, end).replaceAll(" ","");
          }
          return tableStr;
      }
      
      
      public String getTableNameDelete()
      {
          String temp="";
          if(setStyleUpdate()){
              temp=deleteHave.getTableName();
          }
          else{
              temp=deleteNo.getTableName();
          }
          return temp;
      }      
      
      
      //list cac bieu thuc SET//
      public List<String> getLstSet(){
          List<String> listSetTable=new ArrayList<String>();
          
          if(setStyleUpdate()){
              StringToListStringHaveWhere(listSetTable);
          }
          else{
              StringToListStringNoWhere(listSetTable);
          }
          List<String> temp=new ArrayList<String>();
          for(int i=0;i<listSetTable.size();i++)
          {      
                 temp.add(listSetTable.get(i).replaceAll("'","").replaceAll(" ",""));
                  
          }
          return temp;
      }
      
      //Tu List bieu thuc Set chuyen sang list cac value va column!
      public List<String> expressionToSetString(List<String> lstSetTable){
          List<String> lstColumAndValue=new ArrayList<String>();
          String string[];
          for(String s:lstSetTable){
              string=s.split("=");
              for(String str:string){
                  s=str.replaceAll(" ","");
                  lstColumAndValue.add(s);
              }
          }
          return lstColumAndValue;
      }
      
      public void StringToListStringHaveWhere(List<String> lstTable){
          String setString=updateHave.getSetString();
              
              if(multiComponent(setString)){
                  String[] columnAndValue=setString.split(",");
                  String temp="";
                  for(String str:columnAndValue){
                      temp=str.replaceAll(" ","");
                      lstTable.add(temp);
                  }
              }
              else{
                  lstTable.add(setString);
              }
              
      }
      
      public void StringToListStringNoWhere(List<String> lstTable){
          String setString=updateNo.getSetString();
              
              if(multiComponent(setString)){
                  String[] columnAndValue=setString.replaceAll(","," ").split(" ");
                  for(String str:columnAndValue){
                      lstTable.add(str);
                  }
              }
      }
      
      ///da sua xong ham nay
      public List<String> getWhereExpressionUpdate(){
          List<String> lstWhere=new ArrayList<String>();
          String whereStr=updateHave.getWhereString();
          ///Lay and or....
          int ANDindex;
          int ORindex;
         
          while(whereStr.indexOf("AND")>0||whereStr.indexOf("OR")>0){
              ANDindex=whereStr.indexOf("AND");
              ORindex=whereStr.indexOf("OR");
              if(ANDindex < ORindex  && ANDindex > 0){
                  whereStr=whereStr.replaceFirst("AND", ",").replaceAll(" ","");
                  UpdateOperatorList.add("AND");
              }
              else
              if(ORindex < ANDindex && ORindex > 0){
                  
                  whereStr=whereStr.replaceFirst("OR", ",").replaceAll(" ","");
                  UpdateOperatorList.add("OR");
              }
              else
                  if(ANDindex > ORindex && ORindex < 0){
                      whereStr=whereStr.replaceFirst("AND", ",").replaceAll(" ","");
                      UpdateOperatorList.add("AND");
                  }
                  else 
                  if(ORindex > ANDindex && ANDindex < 0){
                      whereStr=whereStr.replaceFirst("OR", ",").replaceAll(" ","");
                      UpdateOperatorList.add("OR");
                  }
                  
          }
          String[] list=whereStr.split(",");
          List<String> lstCondition=new ArrayList<String>();
          for(String s:list){
              
              lstCondition.add(s);
          }
          String[] Operators={"==","!=",">",">=","<","<="};
          String[] lstValueAndOperator=null;
          for(String str:lstCondition){
              String strOperator="";
              for(String operator:Operators){
                  if(str.contains(operator)){
                    strOperator=operator;
                    str=str.replaceFirst(operator," ").trim().replaceAll("'","");
                    lstValueAndOperator=str.split(" ");
                    for(String strTemp:lstValueAndOperator){
                        lstWhere.add(strTemp);
                    }
                  lstWhere.add(strOperator);
                  }
              }        
          }
          return lstWhere;
      }
      
      
      
      public List<String> getlstWhereDelete(){
          List<String> lstWhere=new ArrayList<String>();
          String whereStr=deleteHave.getWhereString();
          ///Lay and or....
          int ANDindex;
          int ORindex;
         
          while(whereStr.indexOf("AND")>0||whereStr.indexOf("OR")>0){
              ANDindex=whereStr.indexOf("AND");
              ORindex=whereStr.indexOf("OR");
              if(ANDindex < ORindex  && ANDindex > 0){
                  whereStr=whereStr.replaceFirst("AND", ",").replaceAll(" ","");
                  DeleteOperatorList.add("AND");
              }
              else
              if(ORindex < ANDindex && ORindex > 0){
                  
                  whereStr=whereStr.replaceFirst("OR", ",").replaceAll(" ","");
                  DeleteOperatorList.add("OR");
              }
              else
                  if(ANDindex > ORindex && ORindex < 0){
                      whereStr=whereStr.replaceFirst("AND", ",").replaceAll(" ","");
                      DeleteOperatorList.add("AND");
                  }
                  else 
                  if(ORindex > ANDindex && ANDindex < 0){
                      whereStr=whereStr.replaceFirst("OR", ",").replaceAll(" ","");
                      DeleteOperatorList.add("OR");
                  }
                  
          }
          String[] list=whereStr.split(",");
          List<String> lstCondition=new ArrayList<String>();
          for(String s:list){
              
              lstCondition.add(s);
          }
          String[] Operators={"==","!=",">",">=","<","<="};
          String[] lstValueAndOperator=null;
          for(String str:lstCondition){
              String strOperator="";
              for(String operator:Operators){
                  if(str.contains(operator)){
                    strOperator=operator;
                    str=str.replaceFirst(operator," ").trim().replaceAll("'","");
                    lstValueAndOperator=str.split(" ");
                    for(String strTemp:lstValueAndOperator){
                        lstWhere.add(strTemp);
                    }
                  lstWhere.add(strOperator);
                  }
              }        
          }
          return lstWhere;
      }
      
      
      public List<String> ArrANDArr(List<String> a,List<String> b){
          List<String> result=new ArrayList<String>();
          for(int i=0;i<a.size();i++){
              for(String j:b){
                  if(a.get(i).equals(j))
                      {
                      result.add(j);
                      break;
                  }
                  
              }
          }
          return result;
      }
      
      public List<String> ArrORArr(List<String> a,List<String> b){
         List<String> result=new ArrayList<String>();
         List<String> tempResult=new ArrayList<String>();
         for(String i:a){
             for(String j:b){
                 if(i.equals(j)){
                     result.add(j);
                     break;
                 }  
             }
         }
         int temp=0;
         for(String i:a){
             temp=0;
             for(String j:result)
             {
                 if(!i.equals(j)){
                     temp++;
                 }
             }
             if(result.size()==temp)
             {
                 tempResult.add(i);                 
             }
         }
         for(String x:b){
             temp=0;
             for(String j:result)
             {
                 if(!x.equals(j))
                 {
                     temp++;
                 }
             }
             if(temp==result.size())
             {
                 tempResult.add(x);
             }
         }
         for(String str:tempResult)
         {
             result.add(str);
         }
         return result;
      }
      
      public boolean multiComponent(String string){
          if(string.contains(",")){
              return true;
          }
          else return false;
      }
      
      
      public List<String> GetIDRowUpdate(List<String> lstWhere,Database database){
          List<String> lstID=new ArrayList<String>();
          List<String> lstResult=new ArrayList<String>();
          try{
             String tableName=tableNameUpdate();
             File file=new File(database.getPath()+"\\Tables\\" + tableName + "\\" + tableName + ".xml");
             DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
             Document document=docBuilder.newDocument();
             document=docBuilder.parse(file); 
              
              String columnName="";
              String value="";
              
              int countID;//dem so ID da nhan dc tu 1 menh de so sanh
              List<Integer> arrCountID=new ArrayList<Integer>();//mang so luong id thoa dk cua cac bieu thuc so sanh!
              for(int k=0;k<lstWhere.size();k=k+3){
                  countID=0;
                 int operator=checkOperator(lstWhere.get(k+2));//index(k+2) la phan tu luu operator!
                   
              
                
                
              NodeList columns= document.getElementsByTagName("column");
              for(int s=0;s<columns.getLength();s++){
                ///Chon Columns
                Node column=columns.item(s);
                 
                Element eColumn=(Element) column; //lay ra tung column thanh element
                
                columnName=eColumn.getAttribute("name");//name column
                boolean checkInt=eColumn.getAttribute("type").equals("int");
                
                NodeList nlRow=eColumn.getElementsByTagName("row");
                  
                for(int j=0;j<nlRow.getLength();j++){
                    Node row=nlRow.item(j);
                    Element eRow=(Element) row;
                    String iD=eRow.getAttribute("id");//id ROW
                    value=eRow.getAttribute("text");//value cua column
                    if(columnName.equals(lstWhere.get(k))){
                    switch(operator)
                    {
                        case 1:
                        {
                            if(checkInt && 
                                    Integer.parseInt(value) ==Integer.parseInt(lstWhere.get(k+1)))
                            {
                                lstID.add(iD);
                                countID++;
                            }
                            if(!checkInt){
                                 if(value.equals(lstWhere.get(k+1)))   
                                 {
                                     lstID.add(iD);
                                 
                                     countID++;
                                 }
                            }
                            break;
                        }
                        case 2:{
                            if(checkInt && 
                                    Integer.parseInt(value) !=Integer.parseInt(lstWhere.get(k+1)))
                            {
                                lstID.add(iD);
                                countID++;
                            }
                            if(!checkInt){
                                 if(!value.equals(lstWhere.get(k+1))){   
                                     lstID.add(iD);
                                     countID++;
                                 }
                            }
                            break;
                        }
                        case 3:{
                            if(checkInt) {
                                if(Integer.parseInt(value)>Integer.parseInt(lstWhere.get(k+1)))
                                {
                                    lstID.add(iD);
                                    countID++;
                                }
                            }
                            if(!checkInt){
                                 if(value.compareTo(lstWhere.get(k+1))>0)   
                                 { lstID.add(iD);
                                    countID++;
                                 }
                            }
                            break;
                        }
                        case 4:{
                            if(checkInt && 
                                    Integer.parseInt(lstWhere.get(k+1)) <= Integer.parseInt(value))
                            {
                                lstID.add(iD);
                                countID++;
                            }
                             if(!checkInt){
                                 if(value.compareTo(lstWhere.get(k+1))>=0)   
                                 {
                                     lstID.add(iD);
                                     countID++;
                                 }
                            }
                            break;
                        }
                        case 5:{
                            if(checkInt && 
                                    Integer.parseInt(lstWhere.get(k+1)) > Integer.parseInt(value))
                            {
                                lstID.add(iD);
                                countID++;
                            }
                             if(!checkInt){
                                 if(value.compareTo(lstWhere.get(k+1))<0)   
                                 {
                                     lstID.add(iD);
                                     countID++;
                                 }
                            }
                            break;
                        }
                        case 6:{
                            if(checkInt && 
                            Integer.parseInt(lstWhere.get(k+1)) >= Integer.parseInt(value))
                            {
                                lstID.add(iD);
                                countID++;
                            }
                             if(!checkInt){
                                 if(value.compareTo(lstWhere.get(k+1))<=0)   
                                 {
                                     lstID.add(iD);
                                     countID++;
                                 }
                            }
                            break;
                        }
                    }
                   }
                }
              }
              arrCountID.add(countID);
          }
            
            
            for(int i=0;i<arrCountID.get(0);i++){
                lstResult.add(lstID.get(i));
            }//gan expression dau tien lam result
            List<String> tempList=new ArrayList<String>();
            List<String> lstCompare=new ArrayList<String>();
            int index=arrCountID.get(0);
            int indexOperator=0;
            for(int i=1;i<arrCountID.size();i++){
                lstCompare.clear();
                for(int j=index;j<index+arrCountID.get(i);j++){
                    lstCompare.add(lstID.get(j));
                }
                    if(UpdateOperatorList.get(indexOperator).equals("AND")){
                        tempList.clear();
                        for(String temp:ArrANDArr(lstResult,lstCompare)){
                            tempList.add(temp);
                        }
                        lstResult.clear();
                        for(String string:tempList){
                            lstResult.add(string);
                        }
                        indexOperator++;
                    }
                    else{
                        tempList.clear();
                        for(String temp:ArrORArr(lstResult,lstCompare)){
                            tempList.add(temp);
                        }
                        lstResult.clear();
                        for(String string:tempList){
                            lstResult.add(string);
                        }
                        indexOperator++;
                    }    
                    index=arrCountID.get(i);
                
            }
          }
          catch(Exception e){
              e.printStackTrace();
          }
          List<String> result=new ArrayList<String>();
          String str="";
          for(String s:lstResult)
          {
              if(s.contains("'")){
                  str=s.replaceAll("'","");
                  result.add(str);
              }
              else
                  result.add(s);
          }
          return result;
          
      }
      public List<String> GetIDRowDelete(List<String> lstWhere,Database database){
          List<String> lstID=new ArrayList<String>();
          List<String> lstResult=new ArrayList<String>();
          try{
             String tableName=getTableNameDelete();
             File file=new File(database.getPath()+"\\Tables\\" + tableName + "\\" + tableName + ".xml");
             DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
             Document document=docBuilder.newDocument();
             document=docBuilder.parse(file); 
              
              String columnName="";
              String value="";
              
              int countID;//dem so ID da nhan dc tu 1 menh de so sanh
              List<Integer> arrCountID=new ArrayList<Integer>();//mang so luong id thoa dk cua cac bieu thuc so sanh!
              for(int k=0;k<lstWhere.size();k=k+3){
                  countID=0;
                 int operator=checkOperator(lstWhere.get(k+2));//index(k+2) la phan tu luu operator!
                   
              
                
                
              NodeList columns= document.getElementsByTagName("column");
              for(int s=0;s<columns.getLength();s++){
                ///Chon Columns
                Node column=columns.item(s);
                 
                Element eColumn=(Element) column; //lay ra tung column thanh element
                
                columnName=eColumn.getAttribute("name");//name column
                boolean checkInt=eColumn.getAttribute("type").equals("int");
                
                NodeList nlRow=eColumn.getElementsByTagName("row");
                  
                for(int j=0;j<nlRow.getLength();j++){
                    Node row=nlRow.item(j);
                    Element eRow=(Element) row;
                    String iD=eRow.getAttribute("id");//id ROW
                    value=eRow.getAttribute("text");//value cua column
                    if(columnName.equals(lstWhere.get(k))){
                    switch(operator)
                    {
                        case 1:
                        {
                            if(checkInt && 
                                    Integer.parseInt(value) ==Integer.parseInt(lstWhere.get(k+1)))
                            {
                                lstID.add(iD);
                                countID++;
                            }
                            if(!checkInt){
                                 if(value.equals(lstWhere.get(k+1)))   
                                 {
                                     lstID.add(iD);
                                 
                                     countID++;
                                 }
                            }
                            break;
                        }
                        case 2:{
                            if(checkInt && 
                                    Integer.parseInt(value) !=Integer.parseInt(lstWhere.get(k+1)))
                            {
                                lstID.add(iD);
                                countID++;
                            }
                            if(!checkInt){
                                 if(!value.equals(lstWhere.get(k+1))){   
                                     lstID.add(iD);
                                     countID++;
                                 }
                            }
                            break;
                        }
                        case 3:{
                            if(checkInt) {
                                if(Integer.parseInt(value)>Integer.parseInt(lstWhere.get(k+1)))
                                {
                                    lstID.add(iD);
                                    countID++;
                                }
                            }
                            if(!checkInt){
                                 if(value.compareTo(lstWhere.get(k+1))>0)   
                                 { lstID.add(iD);
                                    countID++;
                                 }
                            }
                            break;
                        }
                        case 4:{
                            if(checkInt && 
                                    Integer.parseInt(lstWhere.get(k+1)) <= Integer.parseInt(value))
                            {
                                lstID.add(iD);
                                countID++;
                            }
                             if(!checkInt){
                                 if(value.compareTo(lstWhere.get(k+1))>=0)   
                                 {
                                     lstID.add(iD);
                                     countID++;
                                 }
                            }
                            break;
                        }
                        case 5:{
                            if(checkInt && 
                                    Integer.parseInt(lstWhere.get(k+1)) > Integer.parseInt(value))
                            {
                                lstID.add(iD);
                                countID++;
                            }
                             if(!checkInt){
                                 if(value.compareTo(lstWhere.get(k+1))<0)   
                                 {
                                     lstID.add(iD);
                                     countID++;
                                 }
                            }
                            break;
                        }
                        case 6:{
                            if(checkInt && 
                            Integer.parseInt(lstWhere.get(k+1)) >= Integer.parseInt(value))
                            {
                                lstID.add(iD);
                                countID++;
                            }
                             if(!checkInt){
                                 if(value.compareTo(lstWhere.get(k+1))<=0)   
                                 {
                                     lstID.add(iD);
                                     countID++;
                                 }
                            }
                            break;
                        }
                    }
                   }
                }
              }
              arrCountID.add(countID);
          }
            
            
            for(int i=0;i<arrCountID.get(0);i++){
                lstResult.add(lstID.get(i));
            }//gan expression dau tien lam result
            List<String> tempList=new ArrayList<String>();
            List<String> lstCompare=new ArrayList<String>();
            int index=arrCountID.get(0);
            int indexOperator=0;
            for(int i=1;i<arrCountID.size();i++){
                lstCompare.clear();
                for(int j=index;j<index+arrCountID.get(i);j++){
                    lstCompare.add(lstID.get(j));
                }
                    if(DeleteOperatorList.get(indexOperator).equals("AND")){
                        tempList.clear();
                        for(String temp:ArrANDArr(lstResult,lstCompare)){
                            tempList.add(temp);
                        }
                        lstResult.clear();
                        for(String string:tempList){
                            lstResult.add(string);
                        }
                        indexOperator++;
                    }
                    else{
                        tempList.clear();
                        for(String temp:ArrORArr(lstResult,lstCompare)){
                            tempList.add(temp);
                        }
                        lstResult.clear();
                        for(String string:tempList){
                            lstResult.add(string);
                        }
                        indexOperator++;
                    }    
                    index=arrCountID.get(i);
                
            }
          }
          catch(Exception e){
              e.printStackTrace();
          }
          List<String> result=new ArrayList<String>();
          String str="";
          for(String s:lstResult)
          {
              if(s.contains("'")){
                  str=s.replaceAll("'","");
                  result.add(str);
              }
              else
                  result.add(s);
          }
          return result;
          
      }
     public void executeDeleteToXMLNoWhere(Database database){
         try{
             String tableName=this.getTableNameDelete();
             File file=new File(database.getPath()+"\\Tables\\" + tableName + "\\" + tableName + ".xml");
             file.createNewFile();       
         }catch(Exception e){
             e.printStackTrace();
         }
     }
     
     public void executeDeleteToXMLHaveWhere(Database database,List<String> lstID){
          try{
             DocumentUlti dom=new DocumentUlti();
             String tableName=this.getTableNameDelete();
             File file=new File(database.getPath()+"\\Tables\\" + tableName + "\\" + tableName + ".xml");
             DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
             Document document=docBuilder.newDocument();
             document=docBuilder.parse(file); 
             
             NodeList nlColumn=document.getElementsByTagName("column");
             for(int i=0;i<nlColumn.getLength();i++){
                 Element column=(Element) nlColumn.item(i);
                 NodeList nlRow=column.getElementsByTagName("row");
                 for(int j=0;j<nlRow.getLength();j++){
                     Element row=(Element) nlRow.item(i);
                     for(String s:lstID){
                         if(row.getAttribute("id").equals(s)){
                             row.getParentNode().removeChild(row);
                         }
                     }
                 }
             }
           if(file.exists()) {
               file.delete();
           }
          dom.writeDocToFile(file.getPath(), document);
         }
          catch(Exception e){
             e.printStackTrace();
         }
     }
     
     public void executeUpdateToXMLNoWhere(Database database,List<String> lstUpdate){
         try{
             String tableName=tableNameUpdate();
             File file=new File(database.getPath()+"\\Tables\\" + tableName + "\\" + tableName + ".xml");
             DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
             Document doc=docBuilder.newDocument();
             doc=docBuilder.parse(file);
         
         NodeList nlColumns=doc.getElementsByTagName("column");
         DocumentUlti dom=new DocumentUlti();
         for(int i=0;i<nlColumns.getLength();i++){
              Node column=nlColumns.item(i);
              if(column.getNodeType()==Node.ELEMENT_NODE){
                  Element eColumn=(Element) column;
                  for(int j=0;j<lstUpdate.size(); j=j+2){
                  if(eColumn.getAttribute("name").equals(lstUpdate.get(j))){
                      NodeList nlRow=eColumn.getElementsByTagName("row");
                      for(int k=0;k<nlRow.getLength();k++)
                      {
                          Node row=nlRow.item(k);
                          row.setTextContent(lstUpdate.get(j+1));
                      }
                     }
                 }
              }
         }
         
          if(file.exists())
          {
              file.delete();
          }
          dom.writeDocToFile(file.getPath(), doc);
     }catch(Exception e){e.printStackTrace();}
     }
      
      public void executeUpdatetoXMLHaveWhere(Database database,List<String> listID,List<String> listUpdate){
          
          String tableName=tableNameUpdate();
          try{
             
             File file=new File(database.getPath()+"\\Tables\\" + tableName + "\\" + tableName + ".xml");
             DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
             Document doc=docBuilder.newDocument();
             doc=docBuilder.parse(file);
          
          DocumentUlti dom=new DocumentUlti();
          
          String str="";
          Element rootElement=doc.getDocumentElement();
          
              
          
          if(tableName.equals(rootElement.getAttribute("name"))){
          NodeList nlColums=doc.getElementsByTagName("column");
          for(int i=0;i<nlColums.getLength();i++){
              Node column=nlColums.item(i);
             
              Element eColumn=(Element) column;
              for(int j=0;j<listUpdate.size();j=j+2){
                if(listUpdate.get(j).equals(eColumn.getAttribute("name"))){
                    NodeList nlRow=eColumn.getElementsByTagName("row");
                    for(int s=0;s<nlRow.getLength();s++){
                        Element row= (Element)nlRow.item(s);
                        for(String string:listID){
                            if(row.getAttribute("id").equals(string)){
                                row.setAttribute("text",listUpdate.get(j+1));   
                            }
                        }
                    }
                }
             }
          } 
          
        }
          
          if(file.exists())
          {
              file.delete();
          }
          dom.writeDocToFile(file.getPath(), doc);
          }catch(Exception e){e.printStackTrace();}
        }
      
      
      
      
      public int checkOperator(String operator){
          if(operator=="==")
              return 1;
          if(operator=="!=")
              return 2;
          if(operator==">")
              return 3;
          if(operator==">=")
              return 4;
          if(operator=="<")
              return 5;
          if(operator=="<=")
              return 6;
          return 0;
      }
}
